<template>
    <el-dialog @open="open" v-model="isOpen" :title="props.openType==='add'?'添加采购信息':'修改采购信息'" @close="close" style="width: 30%">
        <el-form ref="dataForm" :rules="rules" :model="form" label-width="100px" align="center">
            <el-form-item label="优先级" prop="priority">
                <el-input-number v-model="form.priority" :min="0*1" controls-position="right" />
            </el-form-item>
            <el-form-item label="仓库" prop="wareId">
                <el-select v-model="form.wareId" placeholder="请选择" :disabled="props.openType==='edit'">
                    <el-option v-for="ware in wares" :label="ware.label" :value = ware.value :key="ware.value"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button v-show='props.openType==="add"' type="primary" activity  @click="addWmsPurchase">添加</el-button>
                <el-button v-show="props.openType==='edit'" type="success" activity  @click="updateWmsPurchase">更新</el-button>
                <el-button @click="close" type="success" activity >取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
    import {ref,reactive} from 'vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    const  emits = defineEmits(['close','reload'])

    let dataForm = ref(null)
    let props = defineProps(['openType','id'])
    let form = reactive({})
    let isOpen = ref(false)
    let wares = ref([])
    defineExpose({isOpen})

    const rules = reactive({
            assigneeId: [{required: true,message: '请输入采购人Id', trigger: 'blur'},],
            assigneeName: [{required: true,message: '请输入采购人姓名', trigger: 'blur'},],
            phone: [{required: true,message: '请输入采购人联系方式', trigger: 'blur'},],
            priority: [{required: true,message: '请输入优先级', trigger: 'blur'},],
            status: [{required: true,message: '请输入状态', trigger: 'blur'},],
            wareId: [{required: true,message: '请输入仓库Id', trigger: 'blur'},],
            amount: [{required: true,message: '请输入总金额', trigger: 'blur'},],
    })
    function open(){

        request.get(`/api/wmsPurchase/getAllWareSelect`).then(resp => {
            wares.value.length = 0
            wares.value.push(...resp)
        })

        if(props.openType==='edit'){
            request.get(`/api/wmsPurchase/getWmsPurchaseById/${props.id}`).then(resp => {
                for(let key in resp){
                    form[key] = resp[key]
                }
            })
        }else if(props.openType==="add"){
            for(let key in form){
                delete form[key]
            }
            form.priority = 0
        }
    }

    function close(){
        emits("close")
        dataForm.value.resetFields()//重置表单校验
        for(let key in form){
            delete form[key]
        }
    }

    function addWmsPurchase(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/wmsPurchase/add",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '添加成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
    function updateWmsPurchase(){

        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/wmsPurchase/update",form).then(resp => {
                    emits("close")
                    emits("reload")
                    ElMessage({message: '修改成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
</script>

<style scoped>

</style>